import com.dmotiko.chabeli.*
import com.dmotiko.general.*
import com.dmotiko.util.*
import mx.utils.Delegate

class PhotoPopUp
extends BaseClip {
	
	private var mcPic:MovieClip;
	private var mcPicBack:MovieClip;
	
	private var fadeIn:Function;
	private var fadeOut:Function;
	private var bShowed:Boolean;
		
	private function PhotoPopUp() {
		super();
		var root:PhotoPopUp = this;
		MovieEffects.fadeIn();
		MovieEffects.fadeOut();
		mcPicBack = this.createEmptyMovieClip("mcPicBack", this.getNextHighestDepth());
		mcPicBack.attachMovie("photoPopUpPic_horziontalBack", "back", 1, { _x: 10, _y: 10});
		mcPic = this.createEmptyMovieClip("mcPic", this.getNextHighestDepth());
		picHideEnd();
		ChabeliSite.getApp().addEventListener( ChabeliSite.SECTION_CHANGED, Delegate.create(this, sectionChanged) );
	}
	
	public function sectionChanged() {
		if ( ChabeliSite.getApp().getSection() != ChabeliSite.PRODUCTOS) {
			hide();
		}
	}
	
	public function isShowed():Boolean {
		return bShowed;
	}
	
	private function refreshData():Void {
		data.posY = _ymouse;
		this.show();
	}
	
	public function show() {
		this.bShowed = true;
		this._alpha = 0;
		this._visible = true;
		this.fadeIn( { bind: this, listener: showPhoto, ratio: 5 } );
		
	}
	
	private function showPhoto():Void {
		mcPic = this.attachMovie("photoPopUpPic", "mcPic", mcPic.getDepth());
		if ( data.index == 0) mcPic.mcPrev._visible = false;
		if ( data.index == data.photos.length - 1) mcPic.mcNext._visible = false;
		mcPic._alpha = 0;
		mcPic.addEventListener( PhotoPopUpPic.NEXT_PHOTO, Delegate.create(this, this.nextPhoto) );
		mcPic.addEventListener( PhotoPopUpPic.PREV_PHOTO, Delegate.create(this, this.prevPhoto) );
		mcPic.addEventListener( PhotoPopUpPic.CLOSE, Delegate.create(this, this.hide) );
		mcPic.fadeIn( { bind: this, listener: picShowEnd, ratio: 5 } );
	}
	
	private function picShowEnd():Void {
		mcPic.setData(data.photos[ data.index ]);
	}
	
	public function nextPhoto() {
		data.index ++;
		if (data.index == data.photos.length) data.index = 0;
		showPhoto();
	}
	public function prevPhoto() {
		data.index --;
		if (data.index < 0) data.index = data.photos.length - 1;
		showPhoto();
	}
	
	public function hide() {
		mcPicBack.fadeOut( { ratio: 5 } );
		mcPic.fadeOut( { bind: this, listener: picHideEnd, ratio: 5 } );
	}
	
	private function picHideEnd():Void {
		mcPicBack = this.createEmptyMovieClip("mcPicBack", mcPicBack.getDepth());
		mcPicBack.attachMovie("photoPopUpPic_horziontalBack", "back", 1, { _x: 10, _y: 10});
		mcPic = this.createEmptyMovieClip("mcPic", mcPic.getDepth());
		this._visible = false;
		this.bShowed = false;
	}
	
}